package com.smartHire.controller.User.Address;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Address;
import com.smartHire.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DefaultAddressServlet", value = "/DefaultAddressServlet")
public class DefaultAddressServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String addressId = request.getParameter("addressId");
        String defaultId = request.getParameter("defaultId");
        System.out.println(addressId);
        System.out.println(defaultId);
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            UserService userService = (UserService) ctx.getBean("userService");
            Address address = new Address();

            address.setAddress_Id(Integer.parseInt(addressId));
            address.setDefault_address(1);
            userService.updateDefaultAddress(address);

            address.setAddress_Id(Integer.parseInt(defaultId));
            address.setDefault_address(0);
            userService.updateDefaultAddress(address);


        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
